package demo1;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @ClassDescription:
 * @JdkVersion: 1.8
 * @Author: 李白
 * @Created: 2024/5/16 13:14
 */
public class Test {
    public static void main(String[] args) {
      /*  int[] array = {1,4,2,90,30};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));*/
        Student[] students = new Student[3];
        students[0] = new Student("zhangsan",19);
        students[1] = new Student("lisi",2);
        students[2] = new Student("wangwu",30);
        System.out.println("排序前: "+Arrays.toString(students));
        //AgeComparator ageComparator = new AgeComparator();
        /**
         * sort里引用了ComparableTimSort类里的Sort方法，然后在ComparableTimSort类里，调用Comparable类里的compareTo方法，具体为
         * 要将对象强转为Comparable接口，然后进行比较，所以在下面Student类实现了Comparable接口，就说明students可以被强转，然后调用comparaTo方法时
         * 会调用重写的方法
         * 所以students必须要实现Comparable接口
         */
        Arrays.sort(students);
        /*只要ageComparator是实现comparator(比较器)接口的就可以比较，然后根据AgeComparator中
        重写的方法
        */

        System.out.println("排序后: "+Arrays.toString(students));

    }
    public static void main2(String[] args) {
        Student student1 = new Student("zhangsan",19);
        Student student2 = new Student("lisi",10);
        //System.out.println(student2 > student1);
        //System.out.println(student2.compareTo(student1));

        NameComparator nameComparator = new NameComparator();
        System.out.println(nameComparator.compare(student1, student2));

    }
    public static void main1(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println(a > b);
    }
}
class AgeComparator implements Comparator<Student>{//comparator比较器
    //用于比较对象年龄大小

    @Override
    public int compare(Student o1, Student o2) {//实现了compare方法
        return o1.age - o2.age;
    }
}









class NameComparator implements Comparator<Student>{//Comparator中有一个方法compare，用于比较两个对象，但具体比较对象的哪个成员变量没有写
    //专门创建一个类，用于比较字符串大小
    @Override
    public int compare(Student o1, Student o2) {
        /**
         * 要使用String类的方法，只需使用String类的名称，紧接着使用点操作符（.）和方法名，compareTo就是String类里的方法
         */
        return o1.name.compareTo(o2.name);//NameComparator类里没有comparaTo方法，所以compareTo是String类里的方法，用于比较字符串的大小
    }
}





class Student implements Comparable<Student>{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

 /*   @Override
    public int compareTo(Student o) {
        System.out.println("fffffffffffffffff");
        if (this.age > o.age){//this.age指的是调用compareTo的对象里的成员变量
            return 1;
        }else if(this.age == o.age){
            return 0;
        }else {
            return -1;
        }

    }*/

    @Override
    public int compareTo(Student o) {
        if (this.age > o.age){
            return 1;
        }else if (this.age == o.age){
            return 0;
        }else {
            return -1;
        }
    }
}
